package com.sun.androidexerciseproject.function.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import com.sun.androidexerciseproject.function.model.Book;
import com.sun.androidexerciseproject.function.model.IMyAidlInterface;

import java.util.ArrayList;
import java.util.List;

public class AIDLService extends Service {
    private List<Book> mBookList;
    private final IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub() {
        @Override
        public List<Book> getBookList() throws RemoteException {
            return mBookList;
        }

        @Override
        public boolean addBook(Book book) throws RemoteException {
            if (null == mBookList) {
                mBookList = new ArrayList<>();
            } else {
                mBookList.add(book);
            }
            return true;
        }
    };

    public AIDLService() {
        mBookList = new ArrayList<>();
        initList();
    }

    private void initList() {
        mBookList.add(new Book("毛泽东"));
        mBookList.add(new Book("林彪"));
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mStub;
    }
}
